package com.hzhc.controller.pad;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hzhc.entity.CntrInfo;
import com.hzhc.entity.CntrYard;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrInfoService;
import com.hzhc.service.CntrYardService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.CntrInfoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @ClassName: LoginController
 * @Description:
 * @version 1.0
 * @author: 功不唐捐
 * @Date: 2024-06-03
 */
@RestController
@RequestMapping("/ipad")
@RequiredArgsConstructor
public class IpadStorehouseViewController {

	private final CntrInfoService cntrInfoService;

	private final CntrYardService cntrYardService;

	/**
	 * 获取堆场区域
	 * 
	 * @return
	 */
	@PostMapping("/storehouseViewlayout")
	public Object getStorehouseViewlayout() {
		JSONObject jsonObject = new JSONObject();
		JSONArray objArry = new JSONArray();
		List<CntrYard> cntrYards =  cntrYardService.selectCntrArea();
		for (CntrYard c :cntrYards) {
			List<String> bays =  new ArrayList<>();
			String row =  c.getRowArray();
			List<String> rows =  JSONUtil.parseArray(row).toList(String.class);
			List<CntrYard> cbays =  cntrYardService.selectBayByArea(c.getArea());
			for (CntrYard cbay: cbays) {
				bays.add(cbay.getBay());
			}
			objArry = combinationData(objArry, c.getArea(), bays, rows, c.getLayer());
		}
		jsonObject.put("code", "200");
		jsonObject.put("data", objArry);
		jsonObject.put("totalRow", objArry.size());
		jsonObject.put("success", true);
		return jsonObject;
	}


	/**
	 * 获取堆场区域
	 *
	 * @return
	 */
	@PostMapping("/getBay")
	public Object getBay() {
		JSONObject jsonObject = new JSONObject();
		JSONArray objArry = new JSONArray();

		List<CntrYard> cbays =  cntrYardService.selectBayByArea("A");
		for (CntrYard cbay: cbays) {
			objArry.add(cbay.getBay());
		}

		jsonObject.put("code", "200");
		jsonObject.put("data", objArry);
		jsonObject.put("totalRow", objArry.size());
		jsonObject.put("success", true);
		return jsonObject;
	}

	/**
	 * 获取堆场箱分布
	 * 
	 * @param vo
	 * @return
	 */
	@PostMapping("/getStorehouseCntr")
	public Object getStorehouseCntr(@RequestBody CntrInfoVo vo) {
		JSONObject jsonObject = new JSONObject();
		List<CntrInfo> cntrInfos = cntrInfoService.getIpadZcCntrInfoByBay(vo);
		jsonObject.put("code", "200");
		jsonObject.put("data", cntrInfos);
		jsonObject.put("totalRow", cntrInfos.size());
		jsonObject.put("success", true);
		return jsonObject;
	}

	/**
	 * 获取所有在场箱
	 *
	 * @param json
	 * @return
	 */
	@PostMapping("/getAllCntr")
	public Object getAllCntr(@RequestBody String json) {
		JSONObject object = JSONObject.parseObject(json);
		String cntrNo = object.getString("cntrNo");
		JSONObject jsonObject = new JSONObject();
		List<CntrInfo> cntrInfos = cntrInfoService.getIpadgetAllCntr(cntrNo);
		jsonObject.put("code", "200");
		jsonObject.put("data", cntrInfos);
		jsonObject.put("totalRow", cntrInfos.size());
		jsonObject.put("success", true);
		return jsonObject;
	}

	/**
	 * 阳逻港铁水联运电子库场图各区箱子计数
	 */
	@RequestMapping("/getIpadYardAreaCount")
	public ResultUtil<JSONObject> getIpadYardAreaCount() {
		CntrInfo cntrInfo = new CntrInfo();
		JSONObject list = cntrInfoService.getIpadCntrInfo(cntrInfo);
		return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
	}


	/**
	 * 组装数据
	 * 
	 * @param objArry
	 * @param area
	 * @param bx
	 * @param by
	 * @param bz
	 * @return
	 */
	private JSONArray combinationData(JSONArray objArry, String area, List<String> bx, List<String> by, String bz) {
		JSONObject object = new JSONObject();
		object.put("area", area);
		object.put("x", bx);
		object.put("y", by);
		object.put("z", bz);
		objArry.add(object);
		return objArry;
	}

	/**
	 * 校验箱位是否可用
	 *
	 * @return
	 */
	@PostMapping("/checkBay")
	public Object checkBay(@RequestBody String json) {
		JSONObject object = JSONObject.parseObject(json);
		String bay = object.getString("bay");
		String[] str=bay.split("-");
		int two=Integer.parseInt(str[1]);
		String before=null;
		String after=null;
		if(two<=10){
			before= str[0] +"-"+"0"+(two-1);
			after= str[0] +"-"+"0"+(two+1);
		}else{
			before=str[0]+"-"+(two-1);
			after=str[0]+"-"+(two+1);
		}
		List<String> bayList=new ArrayList<String>();
		Set<String> baySet=new TreeSet<>();
		if(two%2==0){
			//判断偶数  判断前后奇数位
			getBefore(before, baySet);
			getAfter(after, baySet);
		}else {
			if((two+1)%4!=0){
				//判断奇数
				//判断后一位偶数 +1
				getAfter(after, baySet);
			}else{
				//判断前一位偶数 -1
				getBefore(before, baySet);
			}
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", "200");
		jsonObject.put("data", baySet);
		jsonObject.put("success", true);
		return jsonObject;
	}

	private void getAfter(String after, Set<String> baySet) {
		List<CntrInfo> cntrInfoAfter = cntrInfoService.getAfter(after);
		if (!cntrInfoAfter.isEmpty()) {
			for (CntrInfo cntrInfo : cntrInfoAfter) {
				String[] bays = cntrInfo.getBay().split("-");
				int num = Integer.parseInt(bays[1]) - 1;
				if (num < 10) {
					baySet.add(bays[0] + "-0" + num + "-" + bays[2]);
				} else {
					baySet.add(bays[0] + "-" + num + "-" + bays[2]);
				}
			}
		}
	}

	private void getBefore(String before, Set<String> baySet) {
		List<CntrInfo> cntrInfoBefore = cntrInfoService.getBefore(before);
		if (!cntrInfoBefore.isEmpty()) {
			for (CntrInfo cntrInfo : cntrInfoBefore) {
				String[] bays = cntrInfo.getBay().split("-");
				int num = Integer.parseInt(bays[1]) + 1;
				if (num < 10) {
					baySet.add(bays[0] + "-0" + num + "-" + bays[2]);
				} else {
					baySet.add(bays[0] + "-" + num + "-" + bays[2]);
				}
			}
		}
	}
}
